package br.com.it4.zmed.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import br.com.it4.framework.persistence.exception.PersistenceException;
import br.com.it4.framework.util.exception.BusinessException;
import br.com.it4.framework.util.exception.SystemException;
import br.com.it4.zmed.entity.Exame;
import br.com.it4.zmed.filter.ExameFilter;
import br.com.it4.zmed.persistence.ExameDAO;

/**
 * Classe de acesso as funcionalidades de exame.
 * 
 * @author Mozart Silva (mozart.vsilva@gmail.com)
 * 
 */
@Service(value = "exameService")
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ExameServiceImpl implements ExameService {

    /** Interface de acesso ao repositório do exame. */
    @Autowired
    private ExameDAO exameDAO;

    /** {@inheritDoc} */
    public Exame addExame(Exame exame) throws SystemException, BusinessException {
        this.validateAddOrUpdate(exame);

        Exame exameBusca = new Exame();
        exameBusca.setCodigo(exame.getCodigo());

        // Verifica se já existe um exame cadastro com o mesmo código.
        if (this.getExame(exame.getCodigo()) != null) {
            // throw new
            // BusinessException(BusinessCode.SISTEMA_CODIGO_EXISTENTE);
        }

        try {
            return this.exameDAO.add(exame);
        } catch (PersistenceException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    /** {@inheritDoc} */
    public Exame updateExame(Exame exame) throws SystemException, BusinessException {
        this.validateAddOrUpdate(exame);
        try {
            return this.exameDAO.update(exame);
        } catch (PersistenceException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    /** {@inheritDoc} */
    public void removeExame(Exame exame) throws SystemException {
        try {
            this.exameDAO.remove(exame);
        } catch (PersistenceException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /** {@inheritDoc} */
    public Exame getExame(String codigo) throws SystemException {
        Exame exame = null;
        
        try {
            exame = this.exameDAO.get(codigo);
        } catch (PersistenceException e) {
            throw new SystemException(e);
        }

        return exame;
    }

    /** {@inheritDoc} */
    public List<Exame> getExames() throws SystemException {
        List<Exame> exames = null;

        try {
            exames = this.exameDAO.get();
        } catch (PersistenceException e) {
            throw new SystemException(e);
        }

        return exames;
    }

    /** {@inheritDoc} */
    public List<Exame> getExames(ExameFilter filtro, int registroInicio, int registroFim)
            throws SystemException {
        List<Exame> exames = null;

        try {
            exames = this.exameDAO.get(filtro, registroInicio, registroFim);
        } catch (PersistenceException e) {
            throw new SystemException(e);
        }

        return exames;
    }

    /** {@inheritDoc} */
    public Long getExamesCount(ExameFilter filtro) throws SystemException {
        Long count = null;

        try {
            count = this.exameDAO.getCount(filtro);
        } catch (PersistenceException e) {
            throw new SystemException(e);
        }

        return count;
    }

    /**
     * Valida o exame a ser salvo.
     * 
     * @param exame
     *            O exame a ser validado.
     * @throws BusinessException
     *             Exceção caso ocorra erro na validação.
     */
    private void validateAddOrUpdate(Exame exame) throws BusinessException {
        List<String> messages = new ArrayList<String>();

        // if (exame.getCodigo() == null ||
        // exame.getCodigo().trim().isEmpty()) {
        // messages.add(BusinessCode.SISTEMA_CODIGO_OBRIGATORIO);
        // }
        //
        // if (exame.getCodigo() != null &&
        // exame.getCodigo().length() > 10) {
        // messages.add(BusinessCode.SISTEMA_CODIGO_TAMANHO_INVALIDO);
        // }
        //
        // if (exame.getNome() == null ||
        // exame.getNome().trim().isEmpty()) {
        // messages.add(BusinessCode.SISTEMA_NOME_OBRIGATORIO);
        // }
        //
        // if (exame.getNome() != null && exame.getNome().length() >
        // 100) {
        // messages.add(BusinessCode.SISTEMA_NOME_TAMANHO_INVALIDO);
        // }

        if (!messages.isEmpty()) {
            throw new BusinessException(messages);
        }
    }

}